﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace parkingclient
{
    public partial class admin_spot : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //if user is not an admin, redirect to main page
            if (Session["userType"] == null || int.Parse(Session["userType"].ToString()) != 3)
            {
                Response.Redirect("Default.aspx");
            }

            if (Session["user"] == null)
            {
                lblWelcome.Text = "[<a href='user_login.aspx'>Login</a>]   [<a href='registration.aspx'>Register</a>]";
            }
            else if (int.Parse(Session["userType"].ToString()) == 3) //for admin, add link to the admin dashboard
            {
                lblWelcome.Text = "Welcome " + Session["user"] + "   [<a href='admin_dashboard.aspx'>Admin Dashboard</a>]    [<a href='user_logout.aspx'>Logout</a>]";
            }
            else
            {
                lblWelcome.Text = "Welcome " + Session["user"] + "   [<a href='user_logout.aspx'>Logout</a>]";
            }


            loadSpot();

        }

        protected void loadSpot()
        {
            ddlSpot.Items.Clear();

            //load up the spot list
            ParkingService.WebService1 parking = new ParkingService.WebService1();
            
            //get number of floors
            int floorCount = parking.getFloorCount(Int32.Parse(txtGarage.SelectedValue));

            for (int i = 1; i <= floorCount; i++)
            {
                //get the spot list for each floor
                string spotRes = parking.getSpotList(Int32.Parse(txtGarage.SelectedValue), i);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(spotRes);

                XmlNodeList floors = xmlDoc.GetElementsByTagName("floor");

                foreach (XmlNode f in floors)
                {
                    XmlNodeList spots = f.ChildNodes;
                    foreach (XmlNode s in spots)
                    {
                        ddlSpot.Items.Add(new ListItem("Floor " + f.Attributes["number"].Value + " - " + s.Attributes["id"].Value + " (" + s.Attributes["status"].Value + ")", s.Attributes["id"].Value));
                    }
                }
            }

            
        }

        protected void btnBlock_Click(object sender, EventArgs e)
        {
            ParkingService.WebService1 parking = new ParkingService.WebService1();
            bool res = parking.blockSpot(Int32.Parse(ddlSpot.SelectedValue));
            if (res)
            {
                lblStatus.Text = "Spot " + int.Parse(ddlSpot.SelectedValue) + " has been blocked";
            }
            else
            {
                lblStatus.Text = "Spot " + int.Parse(ddlSpot.SelectedValue) + " cannot be blocked";
            }

            loadSpot();
            
        }

        protected void btnUnblock_Click(object sender, EventArgs e)
        {
            ParkingService.WebService1 parking = new ParkingService.WebService1();
            bool res = parking.unblockSpot(int.Parse(ddlSpot.SelectedValue));
            if (res)
            {
                lblStatus.Text = "Spot " + int.Parse(ddlSpot.SelectedValue) + " has been unblocked";

            }
            else
            {
                lblStatus.Text = "Spot " + int.Parse(ddlSpot.SelectedValue) + " cannot be unblocked";
            }

            loadSpot();
        }

        protected void txtGarage_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadSpot();
        }


    }
}